import React from 'react';
import ReactDOM from 'react-dom';
import axios from 'axios'
import { BrowserRouter } from 'react-router-dom';

// import * as proxy from 'http-proxy-middleware';

import { createStore, applyMiddleware } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import CombinedReducers from './redux';
import thunk from 'redux-thunk';
import { Provider } from 'react-redux';

import { message } from 'antd';

import App from './pages/layout';

//添加请求拦截器
axios.interceptors.request.use(function (config) {
  //在发送请求之前做某事
  config.headers['Authorization'] = `Bearer ${localStorage["token"]}`
  return config;
}, function (error) {
  //请求错误时做些事
  // console.log(error);
  return Promise.reject(error);
});

//添加响应拦截器
axios.interceptors.response.use(function (response) {
  //对响应数据做些事
  if (response.headers['authorization'] !== undefined)
    localStorage["token"] = response.headers['authorization'].replace("Bearer ", "")
  return response;
}, function (error) {
  //请求错误时做些事
  message.warning(`${error.response.status}:${error.response.data.toString()}`);
  return Promise.reject(error);
});

axios.defaults.baseURL = '/api/wordbase';

const store = createStore(CombinedReducers, composeWithDevTools(
  applyMiddleware(thunk),
));

//无认证跳转
if (localStorage["token"] === undefined) {
  if (process.env.REACT_APP_ENVIRONMENT === 'test') {
    window.history.pushState({}, "", "/login")
    window.history.go(0)
  }
  if (process.env.NODE_ENV === 'production') {
    window.history.pushState({}, "", "/login")
    window.history.go(0)
  }
  // if (process.env.NODE_ENV === 'production') {
  //   // window.location.href = 'http://localhost:3001/login';
  //   window.location.href = 'http://baidu.com';
  // }
  // if (process.env.NODE_ENV === 'development') {
  //   window.location.href = 'http://baidu.com';
  // }
}
else {
  ReactDOM.render(
    <Provider store={store}>
      <BrowserRouter basename={"/WordBase"}>
        <App />
      </BrowserRouter>
    </Provider>

    ,
    document.getElementById('root')
  )
}

